from django.shortcuts import render
from django.http import HttpResponse
from rest_framework import viewsets
from rest_framework.status import HTTP_201_CREATED, HTTP_404_NOT_FOUND, HTTP_204_NO_CONTENT
from rest_framework.response import Response
from .serializers import StudentsSerializer, ClassesSerializer, IsDeleteStudentsSerializer, IsDeleteClassesSerializer
from .models import Students, Classes
from rest_framework.decorators import action


# Create your views here.
def test(request):
    return HttpResponse("Hello, world. You're at the polls index.")


# class StudentsViewSet(viewsets.ViewSet):
#
#     # 查看所有学生
#     def list(self, request):
#         # 获取学生模型对象
#         students = Students.objects.filter(is_delete=False)
#         # 通过序列器序列化
#         serializer = StudentsSerializer(students, many=True)
#         # 返回JSON数据
#         return Response(serializer.data)
#
#     # 添加学生
#     def create(self, request):
#         # 获取前端数据并转换为JSON格式
#         serializer = StudentsSerializer(data=request.data)
#         # 进行校验，校验失败则报错
#         serializer.is_valid(raise_exception=True)
#         # 校验通过，进行保存
#         serializer.save()
#         # 返回数据
#         return Response(serializer.data, status=HTTP_201_CREATED)
#
#     # 查看指定学生
#     def retrieve(self, request, pk=None):
#         # 尝试寻找id为pk学生
#         try:
#             student = Students.objects.filter(is_delete=False).get(id=pk)
#         except Students.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 若存在进行序列化
#         serializer = StudentsSerializer(student)
#         # 返回数据
#         return Response(serializer.data)
#
#     # 修改学生数据
#     def update(self, request, pk=None):
#         # 尝试寻找id为pk学生
#         try:
#             student = Students.objects.filter(is_delete=False).get(id=pk)
#         except Students.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 进行反序列化
#         serializer = StudentsSerializer(instance=student, data=request.data)
#         # 进行校验
#         serializer.is_valid(raise_exception=True)
#         # 校验通过，进行保存
#         serializer.save()
#         # 返回状态码，修改成功
#         return Response(serializer.data, status=HTTP_201_CREATED)
#
#     def destroy(self, request, pk=None):
#         # 尝试寻找id为pk学生
#         try:
#             student = Students.objects.filter(is_delete=False).get(id=pk)
#         except Students.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 进行删除
#         student.delete()
#         # 返回状态码，删除成功
#         return Response(status=HTTP_204_NO_CONTENT)


# 更强大的学生视图类
class StudentsViewSet(viewsets.ModelViewSet):
    # 获取模型集
    queryset = Students.objects.filter(is_delete=False)
    # 指定序列器
    serializer_class = StudentsSerializer

    @action(methods=['get'], detail=False)
    def get_last(self, request):
        # 获得数据库中符合要求的最后一个数据(后面的代码去根据源码去进行所写)
        last_student = self.get_queryset().last()
        # 进行序列化(也是根据源码所写)
        serializer = self.get_serializer(last_student)
        # 返回数据给前端
        return Response(serializer.data)


# 更强大的班级视图类
class ClassesViewSet(viewsets.ModelViewSet):
    # 指定模型集
    queryset = Classes.objects.filter(is_delete=False)
    # 指定序列器
    serializer_class = ClassesSerializer

    @action(methods=['get'], detail=False)
    def get_last(self, request):
        # 获得数据库中符合要求的最后一个数据(后面的代码去根据源码去进行所写)
        last_class = self.get_queryset().last()
        # 进行序列化(也是根据源码所写)
        serializer = self.get_serializer(last_class)
        # 返回数据给前端
        return Response(serializer.data)


# 用于逻辑删除学生恢复视图类
class IsDeleteStudentsViewSet(viewsets.ViewSet):
    # 查看所有逻辑删除的学生
    def list(self, request):
        # 获取被逻辑删除的学生模型对象
        students = Students.objects.filter(is_delete=True)
        # 序列化数据
        serializer = IsDeleteStudentsSerializer(students, many=True)
        # 返回数据
        return Response(serializer.data)

    # 查看单个被逻辑删除的学生
    def retrieve(self, request, pk=None):
        # 尝试寻找id为pk的学生
        try:
            student = Students.objects.filter(is_delete=True).get(id=pk)
        except Students.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)
        # 进行序列化
        serializer = IsDeleteStudentsSerializer(student)
        # 返回数据
        return Response(serializer.data)

    # 恢复逻辑删除的学生
    def update(self, request, pk=None):
        # 尝试寻找id为pk的学生
        try:
            student = Students.objects.filter(is_delete=True).get(id=pk)
        except Students.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)
        # 进行反序列化
        serializer = IsDeleteStudentsSerializer(instance=student, data=request.data)
        # 进行校验
        serializer.is_valid(raise_exception=True)
        # 校验通过，进行保存
        serializer.save()
        # 返回状态码，修改成功
        return Response(serializer.data, status=HTTP_201_CREATED)


# 用于逻辑删除班级恢复视图类
class IsDeleteClassesViewSet(viewsets.ViewSet):
    # 查看所有逻辑删除的班级
    def list(self, request):
        # 获取被逻辑删除的班级模型对象
        classes = Classes.objects.filter(is_delete=True)
        # 获取被逻辑删除的班级模型对象
        serializer = IsDeleteClassesSerializer(classes, many=True)
        # 获取被逻辑删除的班级模型对象
        return Response(serializer.data)

    # 查看单个被逻辑删除的班级
    def retrieve(self, request, pk=None):
        # 尝试寻找id为pk的班级
        try:
            classes = Classes.objects.filter(is_delete=True).get(id=pk)
        except Classes.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)
        # 进行序列化
        serializer = IsDeleteClassesSerializer(classes)
        # 获取被逻辑删除的班级模型对象
        return Response(serializer.data)

    # 恢复逻辑删除的班级
    def update(self, request, pk=None):
        # 尝试寻找id为pk的班级
        try:
            classes = Classes.objects.filter(is_delete=True).get(id=pk)
        except Classes.DoesNotExist:
            return Response(status=HTTP_404_NOT_FOUND)
        # 进行反序列化
        serializer = IsDeleteClassesSerializer(instance=classes, data=request.data)
        # 进行校验
        serializer.is_valid(raise_exception=True)
        # 校验通过，进行保存
        serializer.save()
        # 获取被逻辑删除的班级模型对象
        return Response(serializer.data, status=HTTP_201_CREATED)
